package thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {
	private Lock lock = new ReentrantLock();
	private int s = 1;
	public void add(){
		lock.lock();
		s+=1;
		System.out.println(s);
		//这行不加的话，另一个线程会死锁
		int c = 1/0;
	}
	
	
	public class MyThread extends Thread{
		private LockDemo ld;
		public MyThread(LockDemo ld){
			this.ld = ld;
		}
		@Override
		public void run() {
			ld.add();
		}
	}
	
	public static void main(String[] args) {
		LockDemo ld = new LockDemo();
		Thread t1 = new LockDemo().new MyThread(ld);
		Thread t2 = new LockDemo().new MyThread(ld);
		t1.start();
		t2.start();
	}
}
